package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.service.MemberAddressService;
import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketAddressExample;
import com.cskaoyan.market.db.mapper.MarketAddressMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author TangHonghao
 * @since 2023/03/20 17:57
 */
public class MemberAddressServiceImpl implements MemberAddressService {


    @Override
    public List<MarketAddress> selectAddress(Integer id, String name) {
        SqlSession session = MyBatisUtil.getSession();
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressMapper addressMapper = session.getMapper(MarketAddressMapper.class);
        if (id != null) {
            marketAddressExample.or().andUserIdEqualTo(id);
            List<MarketAddress> marketAddresses = addressMapper.selectByExample(marketAddressExample);
            return marketAddresses;
        }
        marketAddressExample.or().andNameIsNotNull().andNameLike("%" + name + "%");
        List<MarketAddress> marketAddresses = addressMapper.selectByExampleSelective(marketAddressExample);
        session.commit();
        session.close();
        return marketAddresses;
    }

    @Override
    public List<MarketAddress> selectAddressAll() {
        SqlSession session = MyBatisUtil.getSession();
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressMapper addressMapper = session.getMapper(MarketAddressMapper.class);
        List<MarketAddress> marketAddresses = addressMapper.selectByExample(marketAddressExample);
        return marketAddresses;
    }
}
